<{extend name="Public:public" /}>

<{block name="title"}>插件<{/block}>

<{block name="content"}> 
<div class="content guide with-sidebar plugins-guide">
  
    
      <div id="ad">
  <!-- <script
    async
    type="text/javascript"
    src="//cdn.carbonads.com/carbon.js?serve=CKYIK2QU&placement=vuejsorg"
    id="_carbonads_js">
  </script> -->
  <a href="https://studio.qcloud.coding.net/campaign/favorite-plugins/?utm_source=vue.js&amp;utm_content=ad1116" target="_blank">
    <img src="/images/coding-net-ad.jpg">
  </a>
</div>

    
  
  
    <h1>插件</h1>
  
  
    <p>插件通常会为 Vue 添加全局功能。插件的范围没有限制——一般有下面几种：</p>
<ol>
<li><p>添加全局方法或者属性，如: <a href="https://github.com/karol-f/vue-custom-element" target="_blank" rel="noopener">vue-custom-element</a></p>
</li>
<li><p>添加全局资源：指令/过滤器/过渡等，如 <a href="https://github.com/vuejs/vue-touch" target="_blank" rel="noopener">vue-touch</a></p>
</li>
<li><p>通过全局 mixin 方法添加一些组件选项，如: <a href="https://github.com/vuejs/vue-router" target="_blank" rel="noopener">vue-router</a></p>
</li>
<li><p>添加 Vue 实例方法，通过把它们添加到 Vue.prototype 上实现。</p>
</li>
<li><p>一个库，提供自己的 API，同时提供上面提到的一个或多个功能，如 <a href="https://github.com/vuejs/vue-router" target="_blank" rel="noopener">vue-router</a></p>
</li>
</ol>
<h2 id="使用插件"><a href="#使用插件" class="headerlink" title="使用插件" data-scroll="">使用插件</a></h2><p>通过全局方法 <code>Vue.use()</code> 使用插件。它需要在你调用 <code>new Vue()</code> 启动应用之前完成：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line"><span class="comment">// 调用 `MyPlugin.install(Vue)`</span></span><br><span class="line">Vue.use(MyPlugin)</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue({</span><br><span class="line">  <span class="comment">//... options</span></span><br><span class="line">})</span><br></pre></td></tr></tbody></table></figure>
<p>也可以传入一个选项对象：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">Vue.use(MyPlugin, { <span class="attr">someOption</span>: <span class="literal">true</span> })</span><br></pre></td></tr></tbody></table></figure>
<p><code>Vue.use</code> 会自动阻止多次注册相同插件，届时只会注册一次该插件。</p>
<p>Vue.js 官方提供的一些插件 (例如 <code>vue-router</code>) 在检测到 <code>Vue</code> 是可访问的全局变量时会自动调用 <code>Vue.use()</code>。然而在例如 CommonJS 的模块环境中，你应该始终显式地调用 <code>Vue.use()</code>：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line"><span class="comment">// 用 Browserify 或 webpack 提供的 CommonJS 模块环境时</span></span><br><span class="line"><span class="keyword">var</span> Vue = <span class="built_in">require</span>(<span class="string">'vue'</span>)</span><br><span class="line"><span class="keyword">var</span> VueRouter = <span class="built_in">require</span>(<span class="string">'vue-router'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 不要忘了调用此方法</span></span><br><span class="line">Vue.use(VueRouter)</span><br></pre></td></tr></tbody></table></figure>
<p><a href="https://github.com/vuejs/awesome-vue#components--libraries" target="_blank" rel="noopener">awesome-vue</a> 集合了来自社区贡献的数以千计的插件和库。</p>
<h2 id="开发插件"><a href="#开发插件" class="headerlink" title="开发插件" data-scroll="">开发插件</a></h2><p>Vue.js 的插件应该有一个公开方法 <code>install</code>。这个方法的第一个参数是 <code>Vue</code> 构造器，第二个参数是一个可选的选项对象：</p>
<figure class="highlight js"><table><tbody><tr><td class="code"><pre><span class="line">MyPlugin.install = <span class="function"><span class="keyword">function</span> (<span class="params">Vue, options</span>) </span>{</span><br><span class="line">  <span class="comment">// 1. 添加全局方法或属性</span></span><br><span class="line">  Vue.myGlobalMethod = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line">    <span class="comment">// 逻辑...</span></span><br><span class="line">  }</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 2. 添加全局资源</span></span><br><span class="line">  Vue.directive(<span class="string">'my-directive'</span>, {</span><br><span class="line">    bind (el, binding, vnode, oldVnode) {</span><br><span class="line">      <span class="comment">// 逻辑...</span></span><br><span class="line">    }</span><br><span class="line">    ...</span><br><span class="line">  })</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 3. 注入组件</span></span><br><span class="line">  Vue.mixin({</span><br><span class="line">    created: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line">      <span class="comment">// 逻辑...</span></span><br><span class="line">    }</span><br><span class="line">    ...</span><br><span class="line">  })</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 4. 添加实例方法</span></span><br><span class="line">  Vue.prototype.$myMethod = <span class="function"><span class="keyword">function</span> (<span class="params">methodOptions</span>) </span>{</span><br><span class="line">    <span class="comment">// 逻辑...</span></span><br><span class="line">  }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>

  
  
    <div class="guide-links">
      
      
        <span>← <a href="/v2/guide/render-function.html">渲染函数 &amp; JSX</a></span>
      
      
      
        <span style="float: right;"><a href="/v2/guide/filters.html">过滤器</a> →</span>
      
    </div>
  
  <div class="footer">
      <script src="//m.servedby-buysellads.com/monetization.js" type="text/javascript"></script>
<div class="bsa-cpc"></div>
<script>
  (function(){
    if(typeof _bsa !== 'undefined' && _bsa) {
    _bsa.init('default', 'CKYD62QM', 'placement:vuejsorg', {
      target: '.bsa-cpc',
      align: 'horizontal',
      disable_css: 'true'
    });
      }
  })();
</script>

    发现错误？想参与编辑？
    <a href="https://github.com/vuejs/cn.vuejs.org/blob/master/src/v2/guide/plugins.md" target="_blank">
      在 GitHub 上编辑此页！
    </a>
  </div>
</div>
<{/block}>
